package com.bianguo.zsd.SpringBootDemo.repository;

import com.bianguo.zsd.SpringBootDemo.bean.User;
import com.bianguo.zsd.SpringBootDemo.utils.TextUtils;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * {@link User} {@link Repository}
 */
@Repository
public class UserRepository {

    private final ConcurrentHashMap<Integer, User> repository = new ConcurrentHashMap<>();
    private static final AtomicInteger idGenerator = new AtomicInteger();

    public boolean save(User user) {
        int id = idGenerator.incrementAndGet();
        String token = TextUtils.getUUID();
        user.setUserId(id);
        return repository.put(id, user) == null;
    }

    public Collection<User> findAll() {
        Collection<User> values = repository.values();
        return values;
    }
}
